package com.cet.lxf.oop.test05;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;

/**
 * @program: algorithm
 * @description: 伪随机数和真随机数
 * @author: 陈恩涛
 * @create: 2021-07-22 14:41
 **/
public class Main05 {
    public static void main(String[] args) {
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(random.nextInt());
        }

        System.out.println("======================");
        SecureRandom secureRandom = new SecureRandom();
        for (int i = 0; i < 10; i++) {
            System.out.println(secureRandom.nextInt());
        }

        SecureRandom sr = null;
        try {
            sr = SecureRandom.getInstanceStrong(); // 获取高强度安全随机数生成器
        } catch (NoSuchAlgorithmException e) {
            sr = new SecureRandom(); // 获取普通的安全随机数生成器
        }
        byte[] buffer = new byte[16];
        sr.nextBytes(buffer); // 用安全随机数填充buffer
        System.out.println(Arrays.toString(buffer));
    }
}
